<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_sina_class extends AWS_CONTROLLER
{
	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action['actions'] = array();
		
		return $rule_action;
	}
	
	public function setup()
	{
		if (get_setting('sina_weibo_enabled') != 'Y')
		{
			die;
		}
	}

	function binding_action()
	{
		$oauth = new Services_Weibo_WeiboOAuth(get_setting('sina_akey'), get_setting('sina_skey'));
		
		//$_SESSION['sina_keys'] = $oauth->getRequestToken();
		
		//HTTP::redirect($oauth->getAuthorizeURL($_SESSION['sina_keys']['oauth_token'], false, get_js_url('/account/sina/binding_callback/'));
		HTTP::redirect($oauth->getAuthorizeURL(get_js_url('/account/sina/binding_callback/')));
	}

	function binding_callback_action()
	{	
		//$oauth = new Services_Weibo_WeiboOAuth(get_setting('sina_akey'), get_setting('sina_skey'), $_SESSION['sina_keys']['oauth_token'], $_SESSION['sina_keys']['oauth_token_secret']);
		$oauth = new Services_Weibo_WeiboOAuth(get_setting('sina_akey'), get_setting('sina_skey'));
		
		//$last_key = $oauth->getAccessToken($_REQUEST['oauth_verifier']);
		
		$_SESSION['sina_token'] = $oauth->getAccessToken('code', array(
			'code' => $_GET['code'],
			'redirect_uri' => get_js_url('/account/sina/binding_callback/')
		));
		
		//$client = new Services_Weibo_WeiboClient(get_setting('sina_akey'), get_setting('sina_skey'), $last_key['oauth_token'], $last_key['oauth_token_secret']);
		$client = new Services_Weibo_WeiboClient(get_setting('sina_akey'), get_setting('sina_skey'), $_SESSION['sina_token']['access_token']);
		
		//$sina_profile = $client->verify_credentials();
		$uid_get = $client->get_uid();
		$sina_profile = $client->show_user_by_id($uid_get['uid']);
		
		if ($sina_profile['error'])
		{
			H::redirect_msg('与微博通信出错 (' . $sina_profile['error'] . '), 请重新操作.', "/account/setting/accountbind/");
		}
		
		//添加积分
		//$this->model("integral")->set_user_integral("BIND_WEIBO", $this->user_id, '微博绑定');
		
		//$this->model('sina_weibo')->bind_account($sina_profile, get_js_url('/account/setting/accountbind/'), $this->user_id, $last_key['oauth_token'], $last_key['oauth_token_secret']);
		$this->model('sina_weibo')->bind_account($sina_profile, get_js_url('/account/setting/accountbind/'), $this->user_id);
	
	}

	function del_bind_action()
	{
		$this->model('sina_weibo')->del_users_by_uid($this->user_id);
		
		HTTP::redirect("/account/setting/accountbind/");
	}
}
	